package com.muyi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyi.mapper.CommentMapper;
import com.muyi.mapper.PaperMapper;
import com.muyi.mapper.TagPaperMapper;
import com.muyi.pojo.entity.Comment;
import com.muyi.pojo.entity.Paper;
import com.muyi.pojo.entity.TagPaper;
import com.muyi.pojo.dto.PaperDTO;
import com.muyi.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author VRMMO
* @description 针对表【paper】的数据库操作Service实现
* @createDate 2025-06-11 15:04:41
*/
@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper>
    implements PaperService{

    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private TagPaperMapper tagPaperMapper;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<Paper> list() {
        return paperMapper.list();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer paper_id) {
        // 1. 删除 tag_paper 表中对应记录
        tagPaperMapper.delete(new QueryWrapper<TagPaper>().eq("paper_id", paper_id));

        // 2. 删除 comment 表中对应记录
        commentMapper.delete(new QueryWrapper<Comment>().eq("paper_id", paper_id));

        // 3. 删除 paper 表记录
        paperMapper.deleteById(paper_id);
    }


    /**
     * 在创建文章时会进行标签的选择，这些选择也要更新到tag_paper表中
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(PaperDTO paperDTO) {
        Paper paper = new Paper();
        paper.setPaper_title(paperDTO.getPaper_title());
        paper.setPublish_date(paperDTO.getPublish_date());
        paper.setView_count(paperDTO.getView_count());
        paperMapper.insert(paper);
        Integer paper_id = paper.getPaper_id(); // 插入后数据库生成的 ID

        for (Integer tag_id : paperDTO.getTagIds()) {
            TagPaper tagPaper = new TagPaper();
            tagPaper.setPaper_id(paper_id); // 注意：paper_id 是插入后返回的主键
            tagPaper.setTag_id(tag_id);
            tagPaperMapper.insertAll(tagPaper);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PaperDTO paperDTO) {
        // 更新 paper 表
        Paper paper = new Paper();
        paper.setPaper_id(paperDTO.getPaper_id()); // 必须有 ID
        paper.setPaper_title(paperDTO.getPaper_title());
        paper.setPublish_date(paperDTO.getPublish_date());
        paper.setView_count(paperDTO.getView_count());
        paperMapper.updateById(paper);

        Integer paper_id = paperDTO.getPaper_id();

        // 删除旧的标签关联
        tagPaperMapper.delete(new QueryWrapper<TagPaper>().eq("paper_id", paper_id));

        // 插入新的标签关联
        for (Integer tag_id : paperDTO.getTagIds()) {
            TagPaper tagPaper = new TagPaper();
            tagPaper.setPaper_id(paper_id);
            tagPaper.setTag_id(tag_id);
            tagPaperMapper.insertAll(tagPaper);
        }
    }


    @Override
    public Paper getByPaperId(Integer paper_id) {
        return paperMapper.getByPaperId(paper_id);
    }

}




